<template>
  <div class="box">
    <!-- 选择框 -->
    <div class="choice">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <!-- 顶部两个下拉选择框 -->
        <div class="form">
          <el-form-item
            label="企业名称"
            prop="ename"
            style="margin-right: 60px"
          >
            <el-input
              v-model="ruleForm.ename"
              placeholder="请输入企业名称"
              style="width: 664px"
            ></el-input>
          </el-form-item>
          <el-form-item label="企业地址" prop="address">
            <el-input
              v-model="ruleForm.address"
              placeholder="请输入企业地址"
              style="width: 664px"
            ></el-input>
          </el-form-item>
        </div>
        <div class="form">
          <el-form-item label="企业电话" prop="phone">
            <el-input
              v-model="ruleForm.phone"
              placeholder="请输入企业电话"
              style="width: 664px"
            ></el-input>
          </el-form-item>
          <el-form-item label="企业邮箱 " prop="email">
            <el-input
              v-model="ruleForm.email"
              placeholder="请输入企业邮箱"
              style="width: 664px"
            ></el-input>
          </el-form-item>
        </div>
        <div class="form">
          <el-form-item label="所属行业" prop="type">
            <el-input
              v-model="ruleForm.type"
              placeholder="请输入所属行业"
              style="width: 664px"
            ></el-input>
          </el-form-item>

          <div class="formupload">
            <!--图片上传 -->
            <el-upload
              class="upload-demo"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              ref="upload"
              action="http://10.2.4.21:8088/back/enterprise/uploadenteimg"
              :before-remove="beforeRemove"
              multiple
              :limit="3"
              :on-exceed="handleExceed"
              :file-list="fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </div>
        </div>
        <!-- 文本域 -->
        <el-form-item label="企业宗旨" prop="introduction">
          <el-input
            type="textarea"
            placeholder="请输入企业宗旨"
            maxlength="600"
            :autosize="{ minRows: 4, maxRows: 6 }"
            v-model="ruleForm.introduction"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm', ruleForm)"
          >保存</el-button
        >
        <el-button @click="Jubpage">取消</el-button>
      </el-form>
      <!-- 取消与确定按钮 -->
    </div>
  </div>
</template>

<script>
import { handleAdd } from "@/api/enterprise/index";
import { watch } from "vue";
export default {
  data() {
    return {
      fileList: [{}],
      ruleForm: {
        ename: "",
        address: "",
        type: "",
        email: "",
        url: "",
        introduction: "",
      },
      rules: {
        ename: [{ required: true, message: "请输入企业名称", trigger: "blur" }],
        address: [
          { required: true, message: "请输入企业地址", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入企业手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号格式",
            trigger: "blur",
          },
        ],
        type: [{ required: true, message: "请输入所属企业", trigger: "blur" }],
        study: [{ required: true, message: "请选择学历", trigger: "blur" }],
        enterprise: [
          { required: true, message: "请输入企业类型", trigger: "blur" },
        ],
        email: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请输入邮箱地址"));
              } else if (
                !/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value)
              ) {
                callback(new Error("请输入正确的邮箱地址"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        introduction: [
          { required: true, message: "请输入职位描述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleSuccess(res) {
      console.log(res, "图片请求成功过");
      this.ruleForm.url = res.data;
    },
    submitForm(formName, e) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(e);
          handleAdd(e)
            .then((res) => {
              this.$message({
                message: "添加信息成功",
                type: "success",
              });
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    Jubpage() {
      this.$router.push("/enterprise");
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 32px;
}

.header-container {
  box-sizing: border-box;
  width: 100%;
  height: 49px;
  border-bottom: 1px solid black;
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;

  .topic {
    //字体样式
    font-size: 24px;
    font-weight: 700;
  }
}

.choice {
  width: 100%;
  display: flex;
}

.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .formupload {
    width: 50%;
    padding: 10px 40px;
    box-sizing: border-box;
  }
}

.line {
  margin-left: 10px;
}
</style>